package com.jianyun.jyzs.dao;

import android.content.Context;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.DeleteBuilder;
import com.j256.ormlite.stmt.QueryBuilder;
import com.jianyun.jyzs.bean.EngProjectLocResult;
import com.jianyun.jyzs.db.DatabaseHelper;


import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.jianyun.jyzs.utils.RceLog;

/**
 * Created by mlli on 2017/10/25.
 */

public class EngLocationDao {
    private Context context;
    private Dao<EngProjectLocResult.EngTypeLocBean,Integer> dao;
    private DatabaseHelper helper;

    public EngLocationDao(Context context) {
        this.context = context;
        try {
            helper = DatabaseHelper.getHelper(context);
            dao = helper.getDao(EngProjectLocResult.EngTypeLocBean.class);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void insert(EngProjectLocResult.EngTypeLocBean engineeringResult){
        RceLog.d("lml","插入数据"+engineeringResult.toString());
        try {
            dao.createOrUpdate(engineeringResult);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public List<EngProjectLocResult.EngTypeLocBean> getEngineer(String projectid){
        QueryBuilder<EngProjectLocResult.EngTypeLocBean, Integer> builder = dao.queryBuilder();
        List<EngProjectLocResult.EngTypeLocBean> query = new ArrayList<>();
        try {
            query = builder.where().eq("ProJectId",projectid).query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return query;
    }
    public EngProjectLocResult.EngTypeLocBean getEngineerByid(String id){
        QueryBuilder<EngProjectLocResult.EngTypeLocBean, Integer> builder = dao.queryBuilder();
        List<EngProjectLocResult.EngTypeLocBean> query = new ArrayList<>();
        try {
            builder.where().eq("id",id);
            query = builder.query();
            if (query.size()>0){
                return query.get(0);
            }else {
                return null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    public List<EngProjectLocResult.EngTypeLocBean> getEngineerSearch(String str,String projectid) {
        QueryBuilder<EngProjectLocResult.EngTypeLocBean, Integer> builder = dao.queryBuilder();
        List<EngProjectLocResult.EngTypeLocBean> query = new ArrayList<>();
        try {
            builder.where().like("Position","%"+str+"%").and().eq("ProJectId",projectid);
            query = builder.query();
            if (query.size()>0){
                return query;
            }else {
                return null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    public List<EngProjectLocResult.EngTypeLocBean> getEngineerCommon(String p2id) {
        QueryBuilder<EngProjectLocResult.EngTypeLocBean, Integer> builder = dao.queryBuilder().orderBy("count",false);
        List<EngProjectLocResult.EngTypeLocBean> query = new ArrayList<>();
        try {
            builder.where().eq("ProJectId",p2id).and().ne("count",0);
            query = builder.query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return query;
    }

    public int getEngineerCount() {
        QueryBuilder<EngProjectLocResult.EngTypeLocBean, Integer> builder = dao.queryBuilder();
        List<EngProjectLocResult.EngTypeLocBean> query = new ArrayList<>();
        try {
            query = builder.query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return query.size();
    }

    public void delAll() {
        DeleteBuilder<EngProjectLocResult.EngTypeLocBean, Integer> builder = dao.deleteBuilder();
        try {
            builder.delete();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
